package com.mazha.rabbitmq.controller;

import com.mazha.rabbitmq.common.cache.ChatGlobalCache;
import com.mazha.rabbitmq.common.validated.Register;
import com.mazha.rabbitmq.common.validated.UserList;
import com.mazha.rabbitmq.enums.ErrorCodeEnum;
import com.mazha.rabbitmq.pojo.vo.UserRegisterVO;
import com.mazha.rabbitmq.result.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户列表注册控制器
 *
 * @author leizi
 * @date 2023/12/08
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserRegisterController {

    /**
     * 动态配置mq
     */
    private final RabbitAdmin rabbitAdmin;

    public UserRegisterController(RabbitAdmin rabbitAdmin) {
        this.rabbitAdmin = rabbitAdmin;
    }

    /**
     * 用户注册
     *
     * @param userRegisterVO 用户注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public RestResult userRegister(@Validated({Register.class}) @RequestBody UserRegisterVO userRegisterVO) {
        // 生成用户id
        Long userId = Math.abs(Long.valueOf(userRegisterVO.getName().hashCode()));
        if (ChatGlobalCache.userRegisterCacheMap.containsKey(userId)) {
            return RestResult.failResult(ErrorCodeEnum.USER_EXISTS);
        }
        userRegisterVO.setUserId(userId);
        ChatGlobalCache.userRegisterCacheMap.put(userId, userRegisterVO);
        return RestResult.successResult(userRegisterVO);
    }

    /**
     * 在线用户建立sse连接
     *
     * @param userId 用户id
     * @return sse
     */
    @GetMapping(value = "/sse/{userId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter createSseStream(@PathVariable(value = "userId") Long userId) {
        if (ChatGlobalCache.userRegisterCacheMap.containsKey(userId)) {
            // 设置连接超时时间
            SseEmitter emitter = new SseEmitter(3600000L);
            ChatGlobalCache.userSseCache.put(userId, emitter);
            return emitter;
        }
        return null;
    }


    /**
     * 获取用户列表
     *
     * @param userRegisterVO 用户详情
     * @return 已登录用户列表
     */
    @PostMapping("/userList")
    public RestResult getUserList(@Validated({UserList.class}) @RequestBody UserRegisterVO userRegisterVO) {
        // 校验用户
        if (ChatGlobalCache.userRegisterCacheMap.containsKey(userRegisterVO.getUserId())) {
            List<UserRegisterVO> userRegisterList = ChatGlobalCache.userRegisterCacheMap.values().stream().collect(Collectors.toList());
            return RestResult.successResult(userRegisterList);
        }
        return RestResult.failResult(ErrorCodeEnum.USER_NOT_EXISTS);
    }

}
